package com.example.service.iml;

import com.example.bean.NetInfo;
import com.example.service.CLibrary;
import com.example.util.IpAddressUtil;
import com.example.util.TypeConversionUtil;
import com.sun.jna.ptr.IntByReference;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.text.SimpleDateFormat;
import java.util.*;

public class TestDemo {
    public static void main(String[] args) {

        try {
            SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            System.out.println("开始时间:"+format.format(new Date()));
            Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
            String iddr= IpAddressUtil.getIntranetIpAddress();
            List<String> list=new LinkedList<>();
            if(iddr!=null){
                List<String> ipList=new ArrayList<>();
                for (int i=1;i<=254;i++){
                    String newIp = iddr.substring(0, iddr.lastIndexOf(".") + 1) + i;
                    if(!newIp.equals(iddr)){
                        ipList.add(newIp);

                    }
                }
                if(ipList.size()>0){
                    for(String newIp:ipList) {
                        IntByReference hComm = new IntByReference();
                        IntByReference intByReference = CLibrary.INSTANCE.OpenNetConnection(hComm, newIp, 8000, 1500);
                        String data = TypeConversionUtil.typeConversion(intByReference);
                        System.out.println("第一部分连接设备结果:" + newIp + "," + data + "," + hComm.getValue());
                        if (data == null && hComm.getValue() != 0) {
                            try {
                                NetInfo info = new NetInfo();
                                CLibrary.INSTANCE.GetNetInfo(hComm.getValue(), info);
                                System.out.println("第二部分尝试拉取设备信息:" + newIp + "," + byteArrayToPort(info.getPORT()) + "," + byteToMacAddressString(info.getMAC()));
                                if (byteArrayToPort(info.getPORT()) == 8000) {
                                    System.out.println("发现设备：" + newIp);
                                    list.add(newIp);
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            } catch (Error e) {
                                e.printStackTrace();
                                System.out.println("发生错误,非上位机设备，hComm:" + hComm.getValue() + ",IP:" + newIp);
                            }
                        }
                        if (hComm.getValue() != 0) {
                            try {
                                System.out.println("关闭机器");
                                CLibrary.INSTANCE.CloseDevice(hComm.getValue());
                                //Thread.sleep(1500);
                            } catch (Error e) {
                                e.printStackTrace();
                                System.out.println("发生错误,非上位机设备，hComm:" + hComm.getValue() + ",IP:" + newIp);
                            }
                        }
                    }
                    System.out.println("最终设备列表："+list);
                    System.out.println("最终设备列表数量："+list.size());
                    System.out.println("结束时间:"+format.format(new Date()));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static int byteArrayToPort(byte[] bytes) {
        if (bytes == null || bytes.length != 2) {
            throw new IllegalArgumentException("Byte array must be of length 2 to represent a port number.");
        }
        // 使用位操作合并两个byte，注意可能需要考虑大小端
        int port = (bytes[0] & 0xFF) << 8 | (bytes[1] & 0xFF);
        return port;
    }

    public static String byteToMacAddressString(byte[] macAddress) {
        // 确保byte数组长度为6
        if (macAddress != null && macAddress.length == 6) {
            // 使用String.format()将byte数组格式化为MAC地址的字符串格式
            return String.format(
                    "%02X:%02X:%02X:%02X:%02X:%02X",
                    macAddress[0], macAddress[1], macAddress[2],
                    macAddress[3], macAddress[4], macAddress[5]
            );
        }
        return null; // 如果byte数组不是6个字节，则返回null或抛出异常
    }
}
